package com.example.librarysystem.repository;

import com.example.librarysystem.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Book> searchBooks(String isbn, String name) {
        String sql = "SELECT ISBN, bname, price, ontime, " +
                "fn_GetBookStatus(ISBN) AS status FROM books WHERE 1=1";
        if (!isbn.isEmpty()) sql += " AND ISBN LIKE '%" + isbn + "%'";
        if (!name.isEmpty()) sql += " AND bname LIKE '%" + name + "%'";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Book.class));
    }
}